package com.grass.appointment.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.androidx.p013lv.base.bean.UserInfo;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.interfaces.Key;
import com.androidx.p013lv.base.p015ui.LazyFragment;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.grass.appointment.C2694R;
import com.grass.appointment.adapter.WordOfMouthAdapter;
import com.grass.appointment.bean.WordOfMouth;
import com.grass.appointment.databinding.FragmentAppointmentDataBinding;
import com.grass.appointment.model.UserPhoenixModel;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import java.util.Objects;

public class DataAppointmentFragment extends LazyFragment<FragmentAppointmentDataBinding> implements OnLoadMoreListener {
    private WordOfMouthAdapter adapter;
    private boolean isSelf = false;
    private UserPhoenixModel model;
    private String recordAt = "";
    private int userId;
    private UserInfo userInfo;

    public static DataAppointmentFragment newInstance(int i) {
        Bundle bundle = new Bundle();
        DataAppointmentFragment dataAppointmentFragment = new DataAppointmentFragment();
        bundle.putInt(Key.USER_ID, i);
        dataAppointmentFragment.setArguments(bundle);
        return dataAppointmentFragment;
    }

    @Override // com.androidx.p013lv.base.p015ui.LazyFragment
    public int setFragmentView() {
        return C2694R.C2699layout.fragment_appointment_data;
    }

    @Override // com.androidx.p013lv.base.p015ui.LazyFragment
    public void initViews() {
        ((FragmentAppointmentDataBinding) this.binding).setClick(new OnClick());
        ((FragmentAppointmentDataBinding) this.binding).setFameStatus(0);
        ((FragmentAppointmentDataBinding) this.binding).setNum(0);
        if (getArguments() != null) {
            this.userId = getArguments().getInt(Key.USER_ID);
        }
        if (this.userId == SpUtils.getInstance().getUserInfo().getUserId()) {
            this.isSelf = true;
        }
        ((FragmentAppointmentDataBinding) this.binding).setIsSelf(Boolean.valueOf(this.isSelf));
        UserPhoenixModel userPhoenixModel = (UserPhoenixModel) new ViewModelProvider((ViewModelStoreOwner) Objects.requireNonNull(getActivity())).get(UserPhoenixModel.class);
        this.model = userPhoenixModel;
        userPhoenixModel.operationInfo().observe(this, new Observer<BaseRes<UserInfo>>() {
            /* class com.grass.appointment.fragment.DataAppointmentFragment.C27991 */

            public void onChanged(BaseRes<UserInfo> baseRes) {
                if (baseRes.getCode() == 200) {
                    DataAppointmentFragment.this.userInfo = baseRes.getData();
                    ((FragmentAppointmentDataBinding) DataAppointmentFragment.this.binding).setUserInfo(DataAppointmentFragment.this.userInfo);
                }
            }
        });
        String string = SpUtils.getInstance().getString("domain");
        WordOfMouthAdapter wordOfMouthAdapter = new WordOfMouthAdapter(getActivity());
        this.adapter = wordOfMouthAdapter;
        wordOfMouthAdapter.setDomain(string);
        ((FragmentAppointmentDataBinding) this.binding).refresh.setOnLoadMoreListener(this);
        ((FragmentAppointmentDataBinding) this.binding).recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        ((FragmentAppointmentDataBinding) this.binding).recycler.setAdapter(this.adapter);
        this.model.dataAboutHer().observe(this, new Observer<BaseRes<String>>() {
            /* class com.grass.appointment.fragment.DataAppointmentFragment.C28002 */

            public void onChanged(BaseRes<String> baseRes) {
                if (baseRes.getCode() == 200) {
                    if (DataAppointmentFragment.this.userInfo != null) {
                        DataAppointmentFragment.this.userInfo.setMeetLock(true);
                        ((FragmentAppointmentDataBinding) DataAppointmentFragment.this.binding).setUserInfo(DataAppointmentFragment.this.userInfo);
                        ToastUtils.getInstance().showCorrect("恭喜！解鎖成功");
                        return;
                    }
                    ToastUtils.getInstance().showSigh("用戶信息異常!");
                }
                ToastUtils.getInstance().showWrong(baseRes.getMsg());
            }
        });
        this.model.getWordOfMouthLive().observe(this, new Observer<BaseRes<WordOfMouth>>() {
            /* class com.grass.appointment.fragment.DataAppointmentFragment.C28013 */

            public void onChanged(BaseRes<WordOfMouth> baseRes) {
                ((FragmentAppointmentDataBinding) DataAppointmentFragment.this.binding).setStatusRefresh(1);
                if (baseRes.getCode() == 200) {
                    WordOfMouth data = baseRes.getData();
                    if (data == null || data.getList() == null || data.getList().size() <= 0) {
                        ((FragmentAppointmentDataBinding) DataAppointmentFragment.this.binding).setStatusRefresh(2);
                    } else if (TextUtils.isEmpty(DataAppointmentFragment.this.recordAt)) {
                        ((FragmentAppointmentDataBinding) DataAppointmentFragment.this.binding).setFameStatus(1);
                        ((FragmentAppointmentDataBinding) DataAppointmentFragment.this.binding).setNum(Integer.valueOf(data.getTotal()));
                        DataAppointmentFragment.this.adapter.setData(data.getList());
                    } else {
                        DataAppointmentFragment.this.adapter.setDatasInEnd(data.getList());
                    }
                }
            }
        });
        this.model.setWordOfMouthLive(this.userId, this.recordAt);
    }

    @Override // com.androidx.p013lv.base.p015ui.LazyFragment
    public void onUserVisible() {
        if (this.isSelf) {
            ((FragmentAppointmentDataBinding) this.binding).setUserInfo(SpUtils.getInstance().getUserInfo());
        }
        super.onUserVisible();
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
    public void onLoadMore(RefreshLayout refreshLayout) {
        if (this.adapter.getLastDataInPosition() == null) {
            ((FragmentAppointmentDataBinding) this.binding).setStatusRefresh(2);
            return;
        }
        String recordAt2 = ((WordOfMouth.FeedBackBean) this.adapter.getLastDataInPosition()).getRecordAt();
        this.recordAt = recordAt2;
        this.model.setWordOfMouthLive(this.userId, recordAt2);
    }

    public static void refreshStatus(SmartRefreshLayout smartRefreshLayout, int i) {
        if (i == 1) {
            smartRefreshLayout.finishLoadMore();
        }
        if (i == 2) {
            smartRefreshLayout.finishLoadMoreWithNoMoreData();
        }
    }

    public class OnClick {
        public OnClick() {
        }

        public void btnUnlockNow() {
            if (DataAppointmentFragment.this.userInfo != null) {
                DataAppointmentFragment.this.model.setAction();
            }
        }
    }
}
